package com.ming.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.domain.Order;
import com.ming.sevice.OrderService;
import com.ming.vo.OrderConfirm;
import com.ming.vo.OrderResult;
import com.ming.vo.OrderStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:何元才
 * @Desc:
 * @data:2021-04-09 16:13
 */
@RestController
@RequestMapping("p/myOrder")
@Api(tags = "订单接口管理")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @PostMapping("orderCount")
    @ApiOperation("获取用户的订单状态")
    public ResponseEntity<OrderStatus> getOrderCount(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderStatus orderStatus = orderService.getOrderCount(userId);
        return ResponseEntity.ok(orderStatus);
    }

    @GetMapping("myOrder")
    @ApiOperation("分页查询用户订单详情")
    public ResponseEntity<Page<Order>>  getOrderPage(Page<Order> page,Integer status){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Page<Order> orderPage = orderService.getOrderPage(page, status, userId);
        return ResponseEntity.ok(orderPage);
    }


    // 订单确认的接口
    // 1.入参  1.1 从购物车过来 带购物车ids集合
    //        1.2 从商品进来  带什么进来？prodId  skuId   count
    // 2 出参   对象  包含 用户的默认收货地址  商品集合   商城公用优惠券  留言   订单总金额 优惠 运费
    @PostMapping("confirm")
    @ApiOperation("订单确认接口")
    public ResponseEntity<OrderResult> confirmOrder(@RequestBody OrderConfirm orderConfirm) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderResult orderResult = orderService.orderConfirm(userId, orderConfirm);
        return ResponseEntity.ok(orderResult);
    }

    /**
     * 入参  订单确认返回对象
     * 出参  订单号
     * 前台拿到订单号
     * 发一个请求 根据订单生成支付的预订单
     *
     * @return
     */
    @PostMapping("submit")
    @ApiOperation("订单提交")
    public ResponseEntity<String> submitOrder(@RequestBody OrderResult orderResult) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        String orderNum = orderService.submit(userId, orderResult);
        return ResponseEntity.ok(orderNum);
    }

    /**
     * 根据订单号 获取订单对象
     *
     * @param orderNum
     * @return
     */
    @GetMapping("getOrderByNum")
    Order getOrderByNum(@RequestParam String orderNum) {
        return orderService.getOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderNum)
        );
    }


    /**
     * 根据订单号查询订单支付状态
     *
     * @param orderSn
     * @return
     */
    @GetMapping("query")
    public ResponseEntity<Boolean> queryOrderStatus(@RequestParam String orderSn) {
        int count = orderService.count(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderSn)
                .eq(Order::getStatus, 2)
        );
        return ResponseEntity.ok(count > 0);
    }


    /**
     * 修改订单状态 已经支付
     *
     * @param orderNum
     */
    @PostMapping("changeOrderStatus")
    void changeOrderStatus(@RequestBody String orderNum) {
        orderService.changeOrderStatus(orderNum);
    }


}
